/*
 * @Author: liuxiang
 * @Date: 2022-01-10 14:36:41
 * @LastEditors: liuxiang
 * @LastEditTime: 2022-01-21 16:54:49
 * @Description: file content
 */
import {
  app,
  BrowserWindow,
  Menu,
  globalShortcut,
  ipcMain,
  ipcRenderer,
} from "electron";
import { Window } from "./window/Window";

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== "development") {
  global.__static = require("path")
    .join(__dirname, "/static")
    .replace(/\\/g, "\\\\");
}

let mainWindow;
let recordWindow = "";
let window = new Window();
const winURL =
  process.env.NODE_ENV === "development"
    ? `http://localhost:9988`
    : `file://${__dirname}/index.html`;

function createWindow() {
  //let window = new Window()
  window.listen();
  /**
   * Initial window options
   */
  Menu.setApplicationMenu(null);
  mainWindow = new BrowserWindow({
    height: 800,
    useContentSize: true,
    resizable: true,
    width: 1200,
    title: "录屏",
    icon: "build/icons/uat-icon-128.ico",
    webPreferences: {
      nodeIntegration: true,
      nodeIntegrationInWorker: true,
      webSecurity: false,
      navigateOnDragDrop: true,
      devTools: true,
    },
  });

  mainWindow.loadURL(winURL);
}

// async function createWindow() {
//   let window = new Window()

//   window.listen()
//   window.createWindows({isMainWin: true})
//   window.createTray()
// }

app.disableHardwareAcceleration();

app.on("ready", createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (mainWindow === null) {
    createWindow();
  }
});

//注册快捷键
app.whenReady().then(() => {
  // Register a 'CommandOrControl+Y' shortcut listener.
  globalShortcut.register("CommandOrControl+Shift+k", (e) => {
    mainWindow.webContents.openDevTools();
    console.log("CommandOrControl+Shift+k执行了");
  });
  //开始录制
  globalShortcut.register("CommandOrControl+Shift+b", (e) => {
    console.log("CommandOrControl+Shift+b执行了");
    mainWindow.webContents.send("startRecord");
  });
  //停止录制
  globalShortcut.register("CommandOrControl+Shift+s", (e) => {
    console.log("CommandOrControl+Shift+s执行了");
    if (recordWindow) {
      window.getWindow(recordWindow).webContents.send("stopRecord");
    }
  });
});

// 开始录制
ipcMain.on("recordWindow", (event, arg) => {
  recordWindow = arg;
});

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
